package com.firewolf.oa.action;

import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.firewolf.oa.base.BaseAction;
import com.firewolf.oa.entities.Department;
import com.firewolf.oa.utils.DepartmentUtil;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class DepartmentAction extends BaseAction<Department> {

	private static final long serialVersionUID = -2331070857832607772L;

	/**
	 * 父部门ID
	 */
	private Long parentId;
	/**
	 * 父部门
	 */
	private Department parent;

	/**
	 * 去新增页面，前台需要展示上级部门，所以这里需要查出部门，准备数据
	 */
	public String toAdd() {
		// 获取一级部门，通过一级部门的children可以获取所有的部门信息
		List<Department> deptList = deptService.findTopDepartments();// 查找等级部门
		// 重新组装数据供前台使用，前台只需要id和格式化过了的部门名字
		List<Department> parentList = DepartmentUtil.getAllDepartmentTree("┣", deptList);
		ActionContext.getContext().put("parentList", parentList);
		return "save";
	}

	/**
	 * 去修改页面
	 * 
	 * @return
	 */
	public String toUpdate() {
		try {
			Department dept = deptService.findById(model.getId());
			ActionContext.getContext().getValueStack().push(dept);
			// 获取一级部门，通过一级部门的children可以获取所有的部门信息
			List<Department> deptList = deptService.findTopDepartments();// 查找等级部门
			// 重新组装数据供前台使用，前台只需要id和格式化过了的部门名字
			List<Department> parentList = DepartmentUtil.getAllDepartmentTree("┣", deptList);
			ActionContext.getContext().put("parentList", parentList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "save";
	}

	/**
	 * 去列表页面
	 * 
	 * @return
	 */
	public String list() {
		// 如果没有父ID，去顶级部门，
		List<Department> objList = deptService.findDeptsByParentId(parentId);
		if (parentId != null) {
			try {
				parent = deptService.findById(parentId);
				ActionContext.getContext().put("parent", parent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ActionContext.getContext().put("objList", objList);
		return "list";
	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	public String add() {
		try {
			Department parentDpartment = parentId == null ? null : deptService.findById(parentId);
			model.setParent(parentDpartment);
			deptService.save(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String update() {
		try {
			Department parentDpartment = parentId == null ? null : deptService.findById(parentId);
			model.setParent(parentDpartment);
			deptService.update(getModel());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() {
		try {
			deptService.delete(model.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}

}
